package com.letoken.platform.repo.app.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.letoken.platform.pub.enums.TokenTransactionStatusEnum;
import com.letoken.platform.pub.enums.TokenTransactionTypeEnum;
import com.letoken.platform.repo.app.dao.AppUserAssetsHistoryMapper;
import com.letoken.platform.repo.app.po.AppUserAssetsHistory;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author letokenlabs
 * @date 2022/11/15$
 */
@Log4j2
@Repository
public class AppUserAssetsHistoryManager {

    @Autowired
    private AppUserAssetsHistoryMapper appUserAssetsHistoryMapper;

    public AppUserAssetsHistory queryBySn(Integer userId, String sn) {
        QueryWrapper<AppUserAssetsHistory> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        query.eq("sn", sn);
        AppUserAssetsHistory data = appUserAssetsHistoryMapper.selectOne(query);
        return data;
    }

    public AppUserAssetsHistory queryByHash(Integer userId, String txId) {
        QueryWrapper<AppUserAssetsHistory> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        query.eq("txid", txId);
        AppUserAssetsHistory data = appUserAssetsHistoryMapper.selectOne(query);
        return data;
    }

    public int add(AppUserAssetsHistory item) {
        return appUserAssetsHistoryMapper.insert(item);
    }

    public int update(AppUserAssetsHistory item) {
        return appUserAssetsHistoryMapper.updateById(item);
    }

    public int insertOrUpdate(AppUserAssetsHistory item) {
        AppUserAssetsHistory data = queryByHash(item.getUserId(), item.getTxid());
        if (data == null) {
            return appUserAssetsHistoryMapper.insert(item);
        } else {
            return appUserAssetsHistoryMapper.updateById(item);
        }
    }


    /**
     * 创建提现资产历史交易日志
     *
     * @param userId        用户id
     * @param fromUserName  发送用户名称
     * @param fromAddress   发送用户地址
     * @param tokenId       代币id
     * @param tokenCurrency 代币名称
     * @param uuid          uuid
     * @param amount        提现金额
     * @param now           当前时间
     * @return
     */
    public AppUserAssetsHistory insertHistory(Integer userId, String fromUserName, String fromAddress, Integer tokenId, String tokenCurrency, String uuid, BigDecimal amount, String toAddress, LocalDateTime now) {
        AppUserAssetsHistory appUserAssetsHistory = new AppUserAssetsHistory();
        appUserAssetsHistory.setUserId(userId);
        appUserAssetsHistory.setType(TokenTransactionTypeEnum.WITHDRAW.getValue());
        appUserAssetsHistory.setStatus(TokenTransactionStatusEnum.PENDING.getValue());
        appUserAssetsHistory.setTokenId(tokenId);
        appUserAssetsHistory.setTokenCurrency(tokenCurrency);
        appUserAssetsHistory.setAmount(amount);
        appUserAssetsHistory.setSn(uuid);
        appUserAssetsHistory.setFromUserName(fromUserName);
        appUserAssetsHistory.setFromAddress(fromAddress);
        appUserAssetsHistory.setToAddress(toAddress);
        appUserAssetsHistory.setCreateTime(now);
        appUserAssetsHistory.setUpdateTime(appUserAssetsHistory.getCreateTime());
        return appUserAssetsHistory;
    }
}
